
const app = getApp()
const db = wx.cloud.database();
const utils = require("../../util/util")

Page({


    data: {
        inputValue : "",
        time : 0,
        friend_nickName:'',
        userI1:{},
        userI2:{}
    },

    onLoad :function (options) {
        this.setData({
            recordId : options.id,
            userInfo : app.globalData.userInfo
        })
        this.getChatList()
        this.getFriendInfo()
    },

    onshow: function (options) {
        this.getChatList()
    },

    publishMessage(){
        if (this.data.inputValue == "") {
            wx.showToast({
                icon: "none",
                title: '不能发送空消息',
            })
            return;
        }
        var that = this;
        wx.cloud.database().collection('chat_record').doc(that.data.recordId).get({
            success(res) {
                console.log(res)
                var record = res.data.record;

                var msg = {}
                msg.id = app.globalData.userInfo._id
                msg.text = that.data.inputValue
                msg.time = utils.formatTime(new Date())

                console.log(msg)
                record.push(msg)
                console.log(record)
                wx.cloud.database().collection('chat_record').doc(that.data.recordId).update({
                    data: {
                        record : record
                    },
                    success(res) {
                        console.log(res)
                        // wx.showToast({
                        //   title: '发送成功',
                        // })

                        that.getChatList(),
                        that.setData({
                            inputValue : ''
                        })
                    }
                
                })
            }
        })
    },

    perjump_fri(){
      var that=this
      console.log(this.data.friend_nickName)
      db.collection('Users').where({
        nickName:this.data.friend_nickName
      }).get({
        success:function(res){
        that.setData({
          userI1:res.data[0]
        })
        console.log(that.data.userI1)
        }
      })
      setTimeout(()=>{
        app.globalData.perjumpid=that.data.userI1._id
        console.log(app.globalData.perjumpid)
        wx.navigateTo({
          url: '/pages/personalInfo/per'
        })
      },500)
    },

    perjump_me(){
      console.log('zhoufu')
      app.globalData.perjumpid=app.globalData.userInfo._id
      console.log(app.globalData.perjumpid)
      wx.navigateTo({
        url: '/pages/personalInfo/per'
      })
    },

    handleInput(e) {
        clearTimeout(this.data.time)
        var that = this;
        this.data.time = setTimeout(() => {
            that.getInputValue(e.detail.value)
        }, 200)
    },

    getInputValue(value) {
        this.setData({
            inputValue: value
        })
    },


    getChatList() {
        var that = this;
        wx.cloud.database().collection('chat_record').doc(that.data.recordId).watch({
            onChange: function(snapshot) {
                
                // console.log("123")
                // console.log(snapshot.docs[0].record)
                that.setData({
                    chatList : snapshot.docs[0].record
                })
                that.setData({
                    scrollLast: "toView"
                })
            },
            onError: function(err){
                console.log(err)
            }
        })
    },
    getFriendInfo() {
        var that = this;
        var friend_nickName,friend_avatarUrl;
        wx.cloud.database().collection('chat_record').doc(that.data.recordId).get({
            success(res) {
                console.log(res)
                if (that.data.userInfo._id==res.data.userA_id){
                    
                    friend_nickName=res.data.userB_nickName;
                    friend_avatarUrl = res.data.userB_avatarUrl;
                    console.log("A")
                    
                } else {
                    console.log("B")
                    friend_nickName = res.data.userA_nickName
                    friend_avatarUrl = res.data.userA_avatarUrl
                }
                wx.setNavigationBarTitle({
                    title: friend_nickName
                })
                that.setData({
                  friend_nickName:friend_nickName,
                  friend_avatarUrl:friend_avatarUrl
                })
            }
        })
    }

})